package cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.service.turntable;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntableExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntablePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.dataobject.turntable.TurntableDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.mysql.turntable.TurntableMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 转盘管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TurntableServiceImpl implements TurntableService {

    @Resource
    private TurntableMapper turntableMapper;

    @Override
    public Integer createTurntable(TurntableDO turntable) {
        if (turntable.getUrl()==null)
            turntable.setUrl("https://jockers.oss-cn-beijing.aliyuncs.com/20240427/d8d5d5e6f8eb43f0b8df7e875550af08.png");
        if (turntable.getGroundUrl()==null)
            turntable.setGroundUrl("https://jockers.oss-cn-beijing.aliyuncs.com/20240427/14baa041887a4e27b77d66abf0402676.png");

        if (turntable.getType()==2){
            if (turntableMapper.selectOne(new LambdaQueryWrapper<TurntableDO>().eq(TurntableDO::getType,1))!=null){
                throw new ServiceException(500,"注册轮盘只能存在一个");
            }
        }
        if (turntable.getType()==3){
            if (turntableMapper.selectOne(new LambdaQueryWrapper<TurntableDO>().eq(TurntableDO::getType,3))!=null){
                throw new ServiceException(500,"拼团轮盘只能存在一个");
            }
        }
        // 插入
        turntableMapper.insert(turntable);
        // 返回
        return turntable.getId();
    }

    @Override
    public void updateTurntable(TurntableDO updateReqVO) {
        // 校验存在
        validateTurntableExists(updateReqVO.getId());
        if (updateReqVO.getUrl()==null)
            updateReqVO.setUrl("https://jockers.oss-cn-beijing.aliyuncs.com/20240427/d8d5d5e6f8eb43f0b8df7e875550af08.png");
        if (updateReqVO.getGroundUrl()==null)
            updateReqVO.setGroundUrl("https://jockers.oss-cn-beijing.aliyuncs.com/20240427/14baa041887a4e27b77d66abf0402676.png");

        if (updateReqVO.getType()==2){
            TurntableDO vo = turntableMapper.selectOne(new LambdaQueryWrapper<TurntableDO>().eq(TurntableDO::getType, 1));
            if (vo!=null&& !Objects.equals(updateReqVO.getId(), vo.getId())){
                throw new ServiceException(500,"注册轮盘只能存在一个");
            }
        }
        if (updateReqVO.getType()==3){
            TurntableDO vo = turntableMapper.selectOne(new LambdaQueryWrapper<TurntableDO>().eq(TurntableDO::getType, 3));
            if (vo!=null&& !Objects.equals(updateReqVO.getId(), vo.getId())){
                throw new ServiceException(500,"拼团轮盘只能存在一个");
            }
        }

        // 更新
        turntableMapper.updateById(updateReqVO);
    }

    @Override
    public void deleteTurntable(Integer id) {
        // 校验存在
        validateTurntableExists(id);
        // 删除
        turntableMapper.deleteById(id);
    }

    private void validateTurntableExists(Integer id) {
        if (turntableMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public TurntableDO getTurntable(Integer id) {
        return turntableMapper.selectById(id);
    }

    @Override
    public List<TurntableDO> getTurntableList(Collection<Integer> ids) {
        return turntableMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TurntableDO> getTurntablePage(TurntablePageReqVO pageReqVO) {
        return turntableMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TurntableDO> getTurntableList(TurntableExportReqVO exportReqVO) {
        return turntableMapper.selectList(exportReqVO);
    }

}
